Given two strings ransomNote and magazine, return true if ransomNote can be constructed by using the letters from magazine and false otherwise.
Each letter in magazine can only be used once in ransomNote.
Example 1:
Input: ransomNote = "a", magazine = "b"
Output: false
Example 2:
Input: ransomNote = "aa", magazine = "ab"
Output: false
Example 3:
Input: ransomNote = "aa", magazine = "aab"
Output: true
解題思路
1.因為只會有小寫英文字母,所以可以用一個長度26的整數陣列統計出現次數。
2.遍歷magazine→計算每個字母的出現次數。
3.遍歷ransomNote→消耗對應字母的次數。
4.若有不足(次數 < 0)就代表不能拼出。